var async = require('async')

module.exports = function(app) {
  return new Handler(app)
}

var Handler = function(app) {
  this.app = app
  Handler.app = app
  this.sessionService = this.app.get('sessionService')
  this.channelService = this.app.get('channelService')
}

var handler = Handler.prototype
var local = {}

//获取验证码
handler.sendCaptcha = function(msg,session,next) {
	var phone = msg.phone
	console.log("phone : "+phone)
	this.app.rpc.game.smsServer.sendCaptcha(null,phone,function(flag) {
		next(null,{flag : flag})
	})
}

//发送验证码、身份信息
handler.bindPhone = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var code = msg.code
	var cardNo = msg.cardNo
	var realName = msg.realName
	self.app.rpc.game.smsServer.bindPhone(null,uid,code,cardNo,realName,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}

//注册账号
handler.register = function(msg,session,next) {
	var phone = msg.phone
	var password = msg.password
	var code = msg.code
	var referrer = msg.referrer
	this.app.rpc.game.smsServer.register(null,phone,password,code,referrer,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}



//修改昵称
handler.changeNickname = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var nickname = msg.nickname
	if(!nickname || typeof(nickname) != "string"){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.remote.changeNickname(null,uid,nickname,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}

//修改头像
handler.changeHeadType = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var headType = msg.headType
	if(typeof(headType) != "number"){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.remote.changeHeadType(null,uid,headType,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}

//重置密码
handler.changePassword = function(msg,session,next) {
	var password = msg.password
	var phone = msg.phone
	var code = msg.code
	if(!password || typeof(password) != "string" || !phone || typeof(phone) != "string"){
		next(null,{flag : false})
		return
	}
	this.app.rpc.game.smsServer.changePassword(null,phone,password,code,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}